package com.easyctrl.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.easyctrl.iface.OnBaseFragmentList;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.activity.SettingActivity;
import com.easyctrl.ldy.activity.UserActivity;
import com.easyctrl.ldy.view.BackView;
import com.easyctrl.ui.base.BaseEasyCtrlFragment;

public class BaseFragmentListview extends BaseEasyCtrlFragment implements
		OnClickListener {

	private ListView listView;
	public SettingActivity activity;
	public UserActivity userActivity;
	private BackView backView;
	private String title;
	private TextView titleView;

	private OnBaseFragmentList onBaseFragmentList;

	public void setOnBaseFragmentList(OnBaseFragmentList onBaseFragmentList) {
		this.onBaseFragmentList = onBaseFragmentList;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		onCreateInit();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.base_fragment_listview, null);

		findViewByID(view);

		initWidget(view);

		return view;
	}

	@Override
	public void onClick(View v) {
		onBaseFragmentList.onClick(v);
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	protected void findViewByID(View view) {
		listView = (ListView) view.findViewById(R.id.listview);
		backView = (BackView) view.findViewById(R.id.backview);
		titleView = (TextView) backView.findViewById(R.id.title);
		backView.findViewById(R.id.leftButton).setOnClickListener(this);
	}

	@Override
	protected void initWidget(View view) {
		listView.setAdapter(onBaseFragmentList.onAdapter());

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (onBaseFragmentList != null) {
					onBaseFragmentList.onItemListviewClick(parent, view,
							position, id);
				}
			}
		});

		if (title != null) {
			titleView.setText(title);
		}
	}

	@Override
	protected void onCreateInit() {
		if (this.getActivity() instanceof SettingActivity) {
			activity = (SettingActivity) this.getActivity();
		} else if (this.getActivity() instanceof UserActivity) {
			userActivity = (UserActivity) this.getActivity();
		}

	}
}
